package com.zl.learning.date;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TestDateFormat {
    @Test
    public void test1() {
        LocalDate localDate = LocalDate.now();
        System.out.println("localDate: " + localDate);
    }

    @Test
    public void test2() {
        LocalDate ld1 = LocalDate.of(2017, 7, 20);
        LocalDate ld2 = LocalDate.parse("2017-07-20");
        System.out.println(ld1);
        System.out.println(ld2);
    }

    @Test
    public void test3() {
        //为今天添加一天，也就是获取明天
        LocalDate tomorrow = LocalDate.now().plusDays(1);
        System.out.println(tomorrow);

        //从今天减去一个月
        LocalDate prevMonth = LocalDate.now().minus(1, ChronoUnit.MONTHS);
        System.out.println(prevMonth);

        //下面写两个例子，分别解析日期 2017-07-20，获取每周中的星期和每月中的日
        DayOfWeek thursday = LocalDate.parse("2017-07-20").getDayOfWeek();
        System.out.println("周四: " + thursday);
        int twenty = LocalDate.parse("2017-07-20").getDayOfMonth();
        System.out.println("twenty: " + twenty);

        //试试今年是不是闰年:
        boolean leapYear = LocalDate.now().isLeapYear();
        System.out.println("是否闰年: " + leapYear);
    }

    @Test
    public void test4() {
        //判断是否在日期之前或之后:
        boolean notBefore = LocalDate.parse("2017-07-20").isBefore(LocalDate.parse("2017-07-22"));
        System.out.println("notBefore: " + notBefore);
        boolean isAfter = LocalDate.parse("2017-07-20").isAfter(LocalDate.parse("2017-07-22"));
        System.out.println("isAfter: " + isAfter);

        //获取这个月的第一天:
        LocalDate firstDayOfMonth = LocalDate.parse("2017-07-20").with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("这个月的第一天: " + firstDayOfMonth);
        firstDayOfMonth = firstDayOfMonth.withDayOfMonth(1);
        System.out.println("这个月的第一天: " + firstDayOfMonth);

        //判断今天是否是我的生日，例如我的生日是 2009-07-20
        LocalDate birthday = LocalDate.of(2009, 9, 29);
        MonthDay birthdayMd = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
        MonthDay today = MonthDay.from(LocalDate.now());
        System.out.println("今天是否是我的生日: " + today.equals(birthdayMd));
    }

    @Test
    public void test5() {
        //LocalTime表示一个时间，而不是日期，下面介绍一下它的使用方法。
        //获取现在的时间，输出15:01:22.144
        LocalTime now = LocalTime.now();
        System.out.println("现在的时间: " + now);

        //将一个字符串时间解析为LocalTime，输出15:02
        LocalTime nowTime = LocalTime.parse("15:02");
        System.out.println("时间是: " + nowTime);

    }

    @Test
    public void test6() {
        //使用静态方法of创建一个时间
        LocalTime nowTime = LocalTime.of(15, 2);
        System.out.println("时间是: " + nowTime);

        //使用解析字符串的方式并添加一小时，输出16:02
        LocalTime nextHour = LocalTime.parse("15:02").plus(1, ChronoUnit.HOURS);
        System.out.println("下一个小时: " + nextHour);

        //获取时间的小时、分钟
        int hour = LocalTime.parse("15:02").getHour();
        System.out.println("小时: " + hour);
        int minute = LocalTime.parse("15:02").getMinute();
        System.out.println("分钟: " + minute);

        //我们也可以通过之前类似的API检查一个时间是否在另一个时间之前、之后
        boolean isBefore = LocalTime.parse("15:02").isBefore(LocalTime.parse("16:02"));
        boolean isAfter = LocalTime.parse("15:02").isAfter(LocalTime.parse("16:02"));
        System.out.println("isBefore: " + isBefore);
        System.out.println("isAfter: " + isAfter);

        //在LocalTime类中也将每天的开始和结束作为常量供我们使用:
        System.out.println(LocalTime.MAX);
        System.out.println(LocalTime.MIN);
    }

    @Test
    public void test7() {
        //LocalDateTime是用来表示日期和时间的，这是一个最常用的类之一。
        //获取当前的日期和时间:
        LocalDateTime now = LocalDateTime.now();
        System.out.println("现在: " + now);

        //下面使用静态方法和字符串的方式分别创建LocalDateTime对象
        LocalDateTime localDateTime = LocalDateTime.of(2017, Month.JULY, 20, 15, 18);
        System.out.println(localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE));
        LocalDateTime.parse("2017-07-20T15:18:00");
        //同时`LocalDateTime`也提供了相关API来对日期和时间进行增减操作:
        LocalDateTime tomorrow = now.plusDays(1);
        System.out.println("明天的这个时间: " + tomorrow);
        LocalDateTime minusTowHour = now.minusHours(2);
        System.out.println("两小时前: " + minusTowHour.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

    @Test
    public void test8() {
        //日期格式化
        //在日常开发中我们用到最多的也许就是日期、时间的格式化了，那在Java8种该如何操作呢？
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("默认格式化: " + now);
        System.out.println("自定义格式化: " + now.format(dateTimeFormatter));
        LocalDateTime localDateTime = LocalDateTime.parse("2017-07-20 15:27:44", dateTimeFormatter);
        System.out.println("字符串转LocalDateTime: " + localDateTime);

    }

    @Test
    public void test9() {
        //也可以使用DateTimeFormatter的format方法将日期、时间格式化为字符串
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateString = dateTimeFormatter.format(LocalDateTime.now());
        System.out.println("日期转字符串: " + dateString);
    }

    @Test
    public void test10() {
        //日期周期
        //Period类用于修改给定日期或获得的两个日期之间的区别。
        //给初始化的日期添加5天
        LocalDate initialDate = LocalDate.parse("2017-07-20");
        LocalDate finalDate = initialDate.plus(Period.ofDays(5));
        System.out.println("初始化日期: " + initialDate);
        System.out.println("加日期之后: " + finalDate);

        //周期API中提供给我们可以比较两个日期的差别，像下面这样获取差距天数:
        long between = ChronoUnit.DAYS.between(initialDate, finalDate);
        System.out.println("差距天数: " + between);
    }

    @Test
    public void test11() {
        //与遗留代码转换
        //在之前的代码中你可能出现了大量的Date类，如何将它转换为Java8种的时间类呢？
        //Date和Instant互相转换
        Date date = Date.from(Instant.now());
        Instant instant = date.toInstant();

        //Date转换为LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        //LocalDateTime转Date
        date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date);

        //LocalDate转Date
        date = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println(date);
    }

    @Test
    public void test12() {
        char ch = '\u9998';
        System.out.println(ch);
    }

    @Test
    public void test13() {
        LocalDate date = LocalDate.of(2014, 3, 18);
        int year = date.getYear();
        Month month = date.getMonth();
        int day = date.getDayOfMonth();
        DayOfWeek dow = date.getDayOfWeek();
        int len = date.lengthOfMonth();
        boolean leap = date.isLeapYear();
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        System.out.println(dow);
        System.out.println(len);
        System.out.println(leap);
    }

    @Test
    public void test14() {
        DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
//        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String now = dtf.format(LocalDateTime.now());
        System.out.println(now);
        LocalDateTime dt = LocalDateTime.parse("2018-06-22 10:57:36", dtf);
        System.out.println(dt);
    }

    @Test
    public void test15() {
        Date date = new Date();
        System.out.println(date.getTime());
        System.out.println(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        System.out.println(localDateTime.getMonth().getValue());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek().getValue());
        Date date1 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date1.getTime());
        System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
    }

    @Test
    public void test16() {
        LocalDate date1 = LocalDate.of(2014, 3, 18);
        LocalDate date2 = date1.plusWeeks(1);
        LocalDate date3 = date2.minusYears(3);
        LocalDate date4 = date3.plus(6, ChronoUnit.MONTHS);
        System.out.println(date1);
        System.out.println(date2);
        System.out.println(date3);
        System.out.println(Date.from(date4.atStartOfDay(ZoneId.systemDefault()).toInstant()));
    }

    @Test
    public void test17() throws InterruptedException {
        Instant start = Instant.now();
        TimeUnit.SECONDS.sleep(1);
        Instant end = Instant.now();
        Duration timeElapsed = Duration.between(start, end);
        long millis = timeElapsed.toMillis();
        System.out.println(millis);
    }

    @Test
    public void test18() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime.getMonth().getValue());
        LocalDateTime dateTime2 = dateTime.plusYears(1);
        System.out.println(dateTime2.getYear());
        System.out.println(dateTime2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(dateTime2.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)));
        Duration duration = Duration.between(dateTime, dateTime2);
        System.out.println(duration.toDays());
        Date date = Date.from(dateTime2.atZone(ZoneId.systemDefault()).toInstant());
    }

    @Test
    public void test19() {
        long end = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long end2 = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("");
//        format.parse("").getTime();
        System.out.println(end);
        System.out.println(end2);
    }

    @Test
    public void test20() {
        LocalDateTime localDateTime = LocalDateTime.now().withHour(0).withSecond(0).withMinute(0).withNano(0);
        LocalDateTime localDateTime2 = LocalDateTime.now().withHour(23).withSecond(59).withMinute(59).withNano(0);
        System.out.println(Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
        System.out.println(localDateTime2);
    }
}
